<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="referrer" content="never" />
  <title>综合案例</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }

    .container {
      width: 1200px;
      margin: 0 auto;
    }

    .video video {
      width: 100%;
      padding: 20px 0;
    }

    .elevator {
      position: fixed;
      top: 280px;
      right: 20px;
      z-index: 999;
      background: #fff;
      border: 1px solid #e4e4e4;
      width: 60px;
    }

    .elevator a {
      display: block;
      padding: 10px;
      text-decoration: none;
      text-align: center;
      color: #999;
    }

    .elevator a.active {
      color: #1286ff;
    }

    .outline {
      padding-bottom: 300px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div class="header">
      <a href="http://pip.itcast.cn">
        <img src="https://pip.itcast.cn/img/logo_v3.29b9ba72.png" alt="" />
      </a>
    </div>
    <div class="video big">
      <video src="https://v.itheima.net/LapADhV6.mp4" controls></video>
    </div>
    <div class="intro big">
      <img src="./images/intro.png" alt="" />
    </div>
    <div class="outline big">
      <img src="./images/outline.png" alt="" />
    </div>
    <div class="elevator">
      <a href="javascript:;">视频介绍</a>
      <a href="javascript:;">项目课程</a>
      <a href="javascript:;">课程大纲</a>
    </div>
  </div>
  <script src="./js/lodash.min.js"></script>
  <script>
    const video = document.querySelector('.video video')


    // 1. 记录播放位置：节流
    // 1.1 给视频注册 timeupdate事件 : 当视频播放条发生变化的时候就会触发
    // 1.2 把视频的当前时间currentTime持久化到本地 
    video.addEventListener(`timeupdate`,_.throttle(function () {
      localStorage.setItem(`time`,video.currentTime)
    }),1000)


    // 2. 页面打开让视频播放进度到本地记录的时间位置
    // 2.1 注册loadeddata 事件 ： 等视频准备好了在修改时间
    // 2.2 让视频的当前时间修改为 本地存储的时间
    video.addEventListener(`loadeddata`,function () {
      video.currentTime=localStorage.getItem(`time`)||0
    })


    // 3. 电梯导航激活效果
    //    保证资源加载完毕，高度已经撑开
    window.onload = () => {
      const smalls = document.querySelectorAll('.elevator a')
      const bigs = document.querySelectorAll('.big')
      
      document.addEventListener(`scroll`,_.throttle(() =>{
        const n=document.documentElement.scrollTop
        for (let i = 0; i < bigs.length; i++) {
         if (n>=bigs[i].offsetTop) {
           const old=document.querySelector(`.elevator .active`)
           old&&old.classList.remove(`active`)
           smalls[i].classList.add(`active`)
         }
          
        }
      },200))
    }
  </script>
</body>

</html>